package com.blog.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.blog.pojo.Article;
import com.blog.pojo.Comment;
import com.blog.pojo.Diary;
import com.blog.service.DiaryService;
import com.blog.service.NodeService;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
public class DiaryController {

    @Resource(name = "diaryService")
    private DiaryService diaryService;

    @RequestMapping("/findDiaryMotto")
    public String findDiaryMotto() {
        ArrayList<HashMap<String, String>> diaryMotto = diaryService.findDiaryMotto();
        return JSON.toJSONString(diaryMotto);
    }

    @RequestMapping("/findDiaryCount")
    public int findDiaryCount(String createTime) {
        return diaryService.findDiaryCount(createTime);
    }

    @RequestMapping("/findDiary")
    public ArrayList<Diary> findDiary(int currentPage, String createTime) {
        return diaryService.findDiary(currentPage,createTime);
    }
    @RequestMapping("/findDiaryComment")
    public List<Comment> findDiaryComment(int id) {
        return diaryService.findDiaryComment(id);
    }

    @RequestMapping("/addComment")
    public void addComment(HttpServletRequest request , int id , String content) {
        Comment comment = new Comment();
        comment.setContent(content);
        comment.setTime(new Date());
        comment.setContent(content);

        diaryService.addComment(id,comment);
    }

    //添加文章
    @RequestMapping("/addDiary")
    public void addDiary(String title, String content) throws IOException {

        Diary diary = new Diary();
        Date date = new Date();
        diary.setTitle(title);
        diary.setCreateTime(date);
        diary.setContent(content);

        diaryService.addDiary(diary);
    }
}
